/*
@ auth	hdg
@ email	  hdg1988@gmail.com
@ date	2010-4-14
 桌面按钮的实现，提供ID属性，若ID属性不为空，则表示为具体的文档，文档中的内容可以通过数据库来获取
*/
function desktopMenu_FUN(){
	//继承关系
	baseMenu.call(this);
	this.initHTML=function(){
		//组装HTML结构
		var html='';
		for(var i = 0 ;i<this.menus.length;i++){
			var menu = this.menus[i];
			html+=$format(this.template,[menu.key,menu.id,menu.title,menu.imgSrc,menu.desc]);
		}
	}
	this.template=["<div class='desktop_menu' key='{0}' id='{1}' title='{2}'>",
		"<img src='{3}'/>",
		"<div class='desc'><div >{4}</div></div>"].join("");

}

function contextMenu(){
	baseMenu.call(this);
	var childrenItems={};
	var parentKey=null;
	var menus = null;
	var itemTemplate_def = ["<div class='item {3} mouseout' key='{1}' hasChildren={4}>",
		"<img class='{5}' src='{0}'/>",
		"<div class='desc img_{5}'>{2} {6}</div>",
		"<div class='{1}_parent children'></div>",
		"<div class='children_{4}'></div>",
		"</div>"].join("");
	var menuTemplate_def="<div class='{1} items'>{0}</div>";
	var quickKey_def="<span class='quickKey'>(<span>{0}</span>)</span>";
	var seperateTemplate_def = '<hr/>';
	var init_def = function(_parentKey,_menus){
		parentKey = _parentKey;
		menus =_menus;
		initHTML_def();
		eventBind_def();
	}

	var initHTML_def=function(){
		if(!menus || menus.length<=0)
			return false;
		var html="";
		for(var i = 0 ;i <menus.length ;i++){
			var menu = menus[i];
			var hasItems=(menu.items && menu.items.length>0)?true:false;
			var quickKeyHTML =menu.quickKey &&menu.quickKey.length>0?$format(quickKey_def,[menu.quickKey]):'';
			if(menu.key=='seperate')
				html+=seperateTemplate_def;
			else
				html+=$format(itemTemplate_def,[menu.imgSrc,menu.key,menu.desc,menu.disabled?'disabled':'',hasItems,menu.imgSrc && menu.imgSrc.length>0?'img':'null',quickKeyHTML]);
			//如果有子节点
			if(hasItems){
				childrenItems[menu.key]=menu.items;
			}
			//事件适配器
			if(!menu.key || menu.key.length==0){
				alert('按钮中没有定义KEY或者KEY不合法')	;
				return false;
			}
			EventAdapter.reg(menu.key.toUpperCase(),menu.cmd);
		}
		html=$format(menuTemplate_def,[html,parentKey?parentKey+'_children':'contextmenu']);
		var appendToElem = parentKey?'.'+parentKey+'_parent':'body';
		$(html).appendTo(appendToElem).css({
			position:'absolute',
			zIndex:9999
		}).addClass('div_hide');
	}
	var show=function(event){
		$(".contextmenu").removeClass('div_hide').css({
			top:event.clientY,
			left:event.clientX
		});
	}
	var hide_def=function(){
		if(!hasClassName("div_hide",".contextmenu")){
			var nodes = $(".contextmenu").get(0).childNodes;
			for(var i=0;i<nodes.length;i++)
				menuMouseOut(nodes[i]);
			$(".contextmenu").addClass("div_hide");
		}
	}
	var mouseover_def = function(){
		if(checkHover(event,this)){
			doWithOtherNodes(this);
			$(this).removeClass('mouseout').addClass("mouseover");
			//获取key
			var key = $(this).attr('key');
			if($(this).attr('hasChildren')=='false')
				return false;
			//如果有子节点，构造子节点
			if(!$('.'+key+'_children').get(0)){
				init_def(key,childrenItems[key]);
				$("."+key+"_children").removeClass("div_hide");
				setStyle_def(key);
			}else{
				$("."+key+"_children").removeClass("div_hide");
			}
		}
	}
	var mouseout_def = function(){
		if(checkHover(event,this)){
			var key = $(this).attr('key');
			if(!$("."+key+"_children").get(0))
					$(this).removeClass("mouseover").addClass("mouseout");
		}
	}
	var click_def=function(){
		// 如果是不可点击按钮，直接返回
		if(hasClassName('disabled',this) || $(this).attr('hasChildren')=='true')
			return false;
		hide_def();
		EventAdapter.exec($(this).attr("key").toUpperCase());
	}
	var doWithOtherNodes=function(el){
			var children =el.parentNode.childNodes;
			for(var i=0;i<children.length;i++){
				var child = children[i];
				if(child==el)
					continue ;
				//如果已经是mouseover，则说明上次展开的是这个节点，需要将其子节点都关闭。
				if(hasClassName("mouseover",child)){
					menuMouseOut(child);
					$(child).removeClass("mouseover");
					$(child).addClass("mouseout");
				}
			}
		}
		//某个按钮的mouseout事件
		var menuMouseOut = function(el){
			var key = $(el).attr("key");
			var childrenBoardElem = $("."+key+"_children").get(0);
			//本身的mouseout事件
			if(hasClassName('mouseover',el))
				$(el).removeClass("mouseover").addClass("mouseout");
			//如果没有子按钮
			if(!childrenBoardElem)
				return;
			var children = childrenBoardElem.childNodes;
			for(i=0;i<children.length;i++){
				if(hasClassName("mouseover",children[i])){
					menuMouseOut(children[i]);
					//$(children[i]).removeClass("mouseover").addClass("mouseout");
				}
			}
			//隐藏这个按钮组
			if(!hasClassName('div_hide',childrenBoardElem))
				$(childrenBoardElem).addClass('div_hide');
		}
	var eventBind_def=function(){
		//右键显示
		$(document).bind('contextmenu',function(event){
			event=event || window.event;
			show(event);
			return false;
		});
		//右键隐藏
		$(document).bind('click',hide_def);
		//click事件
		$(".contextmenu .item").bind('click',click_def);
		//item的mouseon事件
		$(".contextmenu .item").bind('mouseover',mouseover_def);
		$(".contextmenu .item").bind('mouseout',mouseout_def);
	}
	var setStyle_def=function(_parentKey){
			//alert(_parentKey)
			var childrenBoard = $("."+_parentKey+"_children");
			var parentBoard = $("."+_parentKey+'_parent');
			childrenBoard.css({
					position:'absolute'
				});
			if(parentBoard.offset().top+childrenBoard.height()-$(window).height()>0){
				childrenBoard.css({
					position:'absolute'
				});
			}
			//alert();
		}
	this.eventBind = eventBind_def;
	this.init=function(){
		init_def(null,this.menus);
	}
	this.hide=hide_def;
	this.menuTemplate=menuTemplate_def;
	this.itemTemplate = itemTemplate_def;
}

